<!DOCTYPE html>
<html lang="cn">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="引言对于开发人员来说树形结构是一种非线性结构，与数组、栈、队列等线性结构相比，复杂度更高。要深入理解和掌握树形结构，亲自绘制树形结构示意图是一种有效的方法，正所谓想要做到心中有“树”，需要自己动手实践。本文介绍一款高效的绘图工具Graphviz，并演示2个使用 Graphviz 绘制树形结构的例子，同时文章还会推荐一些相关的工具，帮助读者更容易地入门。 Graphviz简介Graphviz ，是G">
<meta property="og:type" content="article">
<meta property="og:title" content="使用Graphviz画树形结构示意图">
<meta property="og:url" content="http://example.com/2024/06/08/17-Graphviz/index.html">
<meta property="og:site_name" content="小马向前走">
<meta property="og:description" content="引言对于开发人员来说树形结构是一种非线性结构，与数组、栈、队列等线性结构相比，复杂度更高。要深入理解和掌握树形结构，亲自绘制树形结构示意图是一种有效的方法，正所谓想要做到心中有“树”，需要自己动手实践。本文介绍一款高效的绘图工具Graphviz，并演示2个使用 Graphviz 绘制树形结构的例子，同时文章还会推荐一些相关的工具，帮助读者更容易地入门。 Graphviz简介Graphviz ，是G">
<meta property="og:locale">
<meta property="og:image" content="http://example.com/2024/06/08/17-Graphviz/OrgTree-preview.png">
<meta property="og:image" content="http://example.com/2024/06/08/17-Graphviz/OrgTree-codesnippet.png">
<meta property="og:image" content="http://example.com/2024/06/08/17-Graphviz/OrgMultiLinkTree-preview.png">
<meta property="og:image" content="http://example.com/2024/06/08/17-Graphviz/OrgMultiLineTree-codesnippet.png">
<meta property="og:image" content="http://example.com/2024/06/08/17-Graphviz/vscode-Graphviz-language-support-for-visual-studio-code.png">
<meta property="og:image" content="http://example.com/2024/06/08/17-Graphviz/vscode-Graphviz-Preview.png">
<meta property="og:image" content="http://example.com/2024/06/08/17-Graphviz/qrcode-wechat-web.jpg">
<meta property="article:published_time" content="2024-06-08T13:52:16.000Z">
<meta property="article:modified_time" content="2024-12-18T08:49:00.000Z">
<meta property="article:author" content="maning">
<meta property="article:tag" content="Graphviz">
<meta property="article:tag" content="工具">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/2024/06/08/17-Graphviz/OrgTree-preview.png">

<link rel="canonical" href="http://example.com/2024/06/08/17-Graphviz/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'cn'
  };
</script>

  <title>使用Graphviz画树形结构示意图 | 小马向前走</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">小马向前走</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="cn">
    <link itemprop="mainEntityOfPage" href="http://example.com/2024/06/08/17-Graphviz/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="maning">
      <meta itemprop="description" content="小马向前走">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小马向前走">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          使用Graphviz画树形结构示意图
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2024-06-08 21:52:16" itemprop="dateCreated datePublished" datetime="2024-06-08T21:52:16+08:00">2024-06-08</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2024-12-18 16:49:00" itemprop="dateModified" datetime="2024-12-18T16:49:00+08:00">2024-12-18</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B7%A5%E5%85%B7/" itemprop="url" rel="index"><span itemprop="name">工具</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="引言"><a href="#引言" class="headerlink" title="引言"></a>引言</h1><p>对于开发人员来说树形结构是一种非线性结构，与数组、栈、队列等线性结构相比，复杂度更高。要深入理解和掌握树形结构，亲自绘制树形结构示意图是一种有效的方法，正所谓想要做到心中有“树”，需要自己动手实践。本文介绍一款高效的绘图工具Graphviz，并演示2个使用 Graphviz 绘制树形结构的例子，同时文章还会推荐一些相关的工具，帮助读者更容易地入门。</p>
<h1 id="Graphviz简介"><a href="#Graphviz简介" class="headerlink" title="Graphviz简介"></a>Graphviz简介</h1><p>Graphviz ，是Graph Visualization Software的缩写，是由AT&amp;T实验室精心打造的开源工具包，它在生成各种图形表示方面应用广泛。Graphviz采用DOT语言描述图表，并通过布局引擎解析脚本，自动完成脚本的布局， 此外，它还支持多种丰富的导出格式，包括SVG和PDF等。</p>
<p>我对Graphviz的喜爱源于其自动布局的特性，<strong>它真正实现了“Graph as code”的理念，即像编写代码一样绘制图形</strong>。Graphviz的强大之处在于其“所思即所得”（WYTIWYG，what you think is what you get）的工作方式，这与传统的“所见即所得”（WYSIWYG，what you see is what you get）工具截然不同。普通的所见即所得工具依赖于鼠标拖拽操作，而使用Graphviz时，我的主要职责是编写DOT脚本，只需关注图形中各元素之间的关系，无需分心于布局问题，这让我从排版的繁琐中解放出来。在日常开发工作中，这一点尤为重要，我们希望在构思代码的同时，能够专注于数据结构本身，快速根据我们的思路生成示意图，而不必时刻考虑排版问题。这样的工作方式让我们能够更加专注于工作本身，将注意力集中在问题的核心上。</p>
<h1 id="我的实践"><a href="#我的实践" class="headerlink" title="我的实践"></a>我的实践</h1><p>本节演示2个用Graphviz画树形结构的例子，让大家对Graphviz先有个直观的认识。</p>
<h2 id="实践1-树形结构示意图"><a href="#实践1-树形结构示意图" class="headerlink" title="实践1 树形结构示意图"></a>实践1 树形结构示意图</h2><p>我接到了一个关于树形结构的开发任务，需要将数据按照省，市，区县的结构组织成树形结构， 借助Grapihviz可以很方便的绘制一个树形结构示意图，方便开发者沟通交流，效果图如下。</p>
<img src="/2024/06/08/17-Graphviz/OrgTree-preview.png" alt="OrgTree-preview" style="zoom:50%;">



<p>树形结构图对应的源码如下，可以看到只需要定义节点及节点之间的关系，不需要关心布局。</p>
<p><img src="/2024/06/08/17-Graphviz/OrgTree-codesnippet.png" alt="OrgTree-codesnippet"></p>
<h2 id="实践2-双向链接树形结构示意图"><a href="#实践2-双向链接树形结构示意图" class="headerlink" title="实践2 双向链接树形结构示意图"></a>实践2 双向链接树形结构示意图</h2><p>一般的树形结构从父节点可以找到子节点，但是从子节点找不到父节点，我需要构造一个双向链接的树形结构，也即从子节点也可以找到父节点，例如我想知道市辖区属于哪个地市，则需要通过子节点不断向上遍历直到找到合适的父节点。 下图是用Graphivz绘制的示意图，可以看到父子节点之间的箭头是双向的。</p>
<img src="/2024/06/08/17-Graphviz/OrgMultiLinkTree-preview.png" alt="OrgMultiLinkTree-preview" style="zoom:50%;">



<p>如下是对应的源码，这里箭头的指向为双向。</p>
<p><img src="/2024/06/08/17-Graphviz/OrgMultiLineTree-codesnippet.png" alt="OrgMultiLineTree-codesnippet"></p>
<h1 id="工具推荐"><a href="#工具推荐" class="headerlink" title="工具推荐"></a>工具推荐</h1><p>前几节介绍了Graphviz以及使用它的原因， 并且展示了我用它画树形结构的实例，本节介绍一些具体的工具，方便大家入门。</p>
<p><strong>推荐1 Graphviz 命令行工具</strong></p>
<p>Graphviz其实是一个命令行工具，使用它之前需要先安装，不同的操作系统安装方式不同，具体可查看官网 <a target="_blank" rel="noopener" href="https://graphviz.org/">https://graphviz.org/</a></p>
<ul>
<li>如果是mac可通过<code>homebrew</code>或者<code>port</code>安装</li>
<li>如果是windows可直接下载安装包</li>
<li>如果是linux根据不同的发行版用不同的包管理软件安装 如<code>apt</code>，<code>yum</code></li>
</ul>
<p>如下是用Graphviz命令将dot源文件生成图片的命令示例</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dot -Tpng input.dot -o output.png</span><br></pre></td></tr></table></figure>



<p><strong>推荐2　vscode插件</strong></p>
<p>Graphviz 是一个强大的命令行工具，但使用起来可能不太方便。幸运的是，VSCode 提供了一些插件，可以显著改善用户体验，使使用 Graphviz 更加直观和友好，下面介绍一些好用的插件。</p>
<ol>
<li>Graphviz language support for visual studio code  </li>
</ol>
<p>提供语法高亮</p>
<p><img src="/2024/06/08/17-Graphviz/vscode-Graphviz-language-support-for-visual-studio-code.png" alt="vscode-Graphviz-language-support-for-visual-studio-code"></p>
<ol start="2">
<li>Graphviz Preview</li>
</ol>
<p>可以快速预览或者导出graphviz文件，这样就不需要记忆具体的命令了。</p>
<p><img src="/2024/06/08/17-Graphviz/vscode-Graphviz-Preview.png" alt="vscode-Graphviz-Preview"></p>
<p><strong>推荐3 python接口</strong></p>
<p>很多编程语言会集成 Graphviz 绘图工具，PyGraphviz是Graphviz的Python接口，允许开发者使用Python代码与Graphviz进行交互，实现图形的创建、布局、可视化和分析</p>
<p>官网 <a target="_blank" rel="noopener" href="https://github.com/pygraphviz/pygraphviz">https://github.com/pygraphviz/pygraphviz</a></p>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>通过掌握基本的 DOT 语言和 Graphviz 工具，用户可以轻松创建和自定义自己的图形，满足常见的可视化需求。正所谓一图胜千言，不论是在学术研究、软件开发还是日常工作中，Graphviz 都能够提供极大的便利和帮助，赶快实践起来，体验它的强大功能吧！</p>
<hr>
<p>微信端的朋友也可关注我的公众号</p>
<p><img src="/2024/06/08/17-Graphviz/qrcode-wechat-web.jpg" alt="qrcode-12cm"></p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Graphviz/" rel="tag"># Graphviz</a>
              <a href="/tags/%E5%B7%A5%E5%85%B7/" rel="tag"># 工具</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2024/02/02/16-shell-wait/" rel="prev" title="shell 并行运行程序技巧">
      <i class="fa fa-chevron-left"></i> shell 并行运行程序技巧
    </a></div>
      <div class="post-nav-item">
    <a href="/2025/05/04/18-elasticdump/" rel="next" title="轻松搞定 Elasticsearch 数据迁移：Elasticdump 助你一臂之力">
      轻松搞定 Elasticsearch 数据迁移：Elasticdump 助你一臂之力 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%BC%95%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">引言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Graphviz%E7%AE%80%E4%BB%8B"><span class="nav-number">2.</span> <span class="nav-text">Graphviz简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E5%AE%9E%E8%B7%B5"><span class="nav-number">3.</span> <span class="nav-text">我的实践</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9E%E8%B7%B51-%E6%A0%91%E5%BD%A2%E7%BB%93%E6%9E%84%E7%A4%BA%E6%84%8F%E5%9B%BE"><span class="nav-number">3.1.</span> <span class="nav-text">实践1 树形结构示意图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9E%E8%B7%B52-%E5%8F%8C%E5%90%91%E9%93%BE%E6%8E%A5%E6%A0%91%E5%BD%A2%E7%BB%93%E6%9E%84%E7%A4%BA%E6%84%8F%E5%9B%BE"><span class="nav-number">3.2.</span> <span class="nav-text">实践2 双向链接树形结构示意图</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%B7%A5%E5%85%B7%E6%8E%A8%E8%8D%90"><span class="nav-number">4.</span> <span class="nav-text">工具推荐</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">5.</span> <span class="nav-text">总结</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">maning</p>
  <div class="site-description" itemprop="description">小马向前走</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">18</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">maning</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
